package com.jlee.android.pimclient.activity;

import com.jlee.android.pimclient.PIMClientConstant;
import com.jlee.android.pimclient.R;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.view.View;
import android.view.View.OnClickListener;

public class PIMClientContactSettingActivity extends Activity {
	CheckBox mOrgnizationCompanyNameCB;
	CheckBox mOrgnizationJobTitleCB;
	CheckBox mNameCB;
	CheckBox mDisplayNameCB;
	CheckBox mNotesCB;
	CheckBox mImCB;
	CheckBox mPersonalMailCB;
	CheckBox mBusinessMailCB;
	CheckBox mMobileCB;
	CheckBox mHomePhoneCB;
	CheckBox mWorkPhoneCB;
	CheckBox mHomeFaxCB;
	CheckBox mWorkFaxCB;
	CheckBox mPictureCB;
	
	SharedPreferences mSettings;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.contact_setting);
		setTitle(R.string.title_contact_setting);

		mSettings = getSharedPreferences(PIMClientConstant.PREFS_NAME, MODE_PRIVATE);
		
		mOrgnizationCompanyNameCB = (CheckBox) findViewById(R.id.orgnizationCompanyName);
		mOrgnizationJobTitleCB = (CheckBox) findViewById(R.id.orgnizationJobTitle);
		mNameCB = (CheckBox) findViewById(R.id.name);
		mDisplayNameCB = (CheckBox) findViewById(R.id.displayName);
		mNotesCB = (CheckBox) findViewById(R.id.notes);
		mImCB = (CheckBox) findViewById(R.id.im);
		mPersonalMailCB = (CheckBox) findViewById(R.id.personalMail);
		mBusinessMailCB = (CheckBox) findViewById(R.id.businessMail);
		mMobileCB = (CheckBox) findViewById(R.id.mobile);
		mHomePhoneCB = (CheckBox) findViewById(R.id.homePhone);
		mWorkPhoneCB = (CheckBox) findViewById(R.id.workPhone);
		mHomeFaxCB = (CheckBox) findViewById(R.id.homeFax);
		mWorkFaxCB = (CheckBox) findViewById(R.id.workFax);
		mPictureCB = (CheckBox) findViewById(R.id.picture);
		
		final Button saveButton = (Button) findViewById(R.id.save);
		saveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				doSave();
			}
			
		});
		
		initChecked();
	}

	private void initChecked() {
		mOrgnizationCompanyNameCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_COMPANYNAME, true));
		mOrgnizationJobTitleCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_JOBTITLE, true));
		mNameCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_NAME, true));
		mDisplayNameCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_DISPLAYNAME, true));
		mNotesCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_NOTES, true));
		mImCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_IM, true));
		mPersonalMailCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_PERSONALMAIL, true));
		mBusinessMailCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_BUSINESSMAIL, true));
		mMobileCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_MOBILE, true));
		mHomePhoneCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_HOMEPHONE, true));
		mWorkPhoneCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_WORKPHONE, true));
		mHomeFaxCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_HOMEFAX, true));
		mWorkFaxCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_WORKFAX, true));
		mPictureCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CONTACT_PICTURE, true));
	}
	
	void doSave() {
		Editor editor = mSettings.edit();

		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_COMPANYNAME, 
				mOrgnizationCompanyNameCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_JOBTITLE, 
				mOrgnizationJobTitleCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_NAME, 
				mNameCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_DISPLAYNAME, 
				mDisplayNameCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_NOTES, 
				mNotesCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_IM, 
				mImCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_PERSONALMAIL, 
				mPersonalMailCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_BUSINESSMAIL, 
				mBusinessMailCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_MOBILE, 
				mMobileCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_HOMEPHONE, 
				mHomePhoneCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_WORKPHONE, 
				mWorkPhoneCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_HOMEFAX, 
				mHomeFaxCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_WORKFAX, 
				mWorkFaxCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CONTACT_PICTURE, 
				mPictureCB.isChecked());
		
		editor.commit();
		finish();
	}
}
